//
//  XFDRegisterThreeView.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/6.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDRegisterThreeView.h"
#import "XFDLogInInputView.h"
#import <Masonry/Masonry.h>
#import <XFDCommon/HBCommon.h>
#import <XFDCommon/NSDictionary+HBSafely.h>
#import <BlocksKit/UIControl+BlocksKit.h>

@interface XFDRegisterThreeView ()

//@property (nonatomic, strong)XFDLogInInputView *inputView;
@property (nonatomic, strong)UIButton *sureButton;
@property (nonatomic, strong)UILabel *titleNavLabel;
@property (nonatomic, strong)UILabel *titleLabel;
//@property (nonatomic, strong)UILabel *titleUserNameLabel;
@property (nonatomic, strong)UILabel *titleNameLabel;
@property (nonatomic, strong)XFDLogInInputView *inputNameView;
@property (nonatomic, strong)UILabel *titlePassWordLabel;
@property (nonatomic, strong)XFDLogInInputView *inputPassWordView;
@end
@implementation XFDRegisterThreeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)inputViewResignFirstResponder{
    [self.inputView inputViewResignFirstResponder];
}
- (void)inputViewBecomeFirstResponder{
    [self.inputView inputViewBecomeFirstResponder];
}
- (void)createView{
    
//    [self addSubview:self.inputView];
    [self addSubview:self.sureButton];
    [self addSubview:self.titleNavLabel];
    [self addSubview:self.titleLabel];
//    [self addSubview:self.titleUserNameLabel];
    [self addSubview:self.titleNameLabel];
    [self addSubview:self.titlePassWordLabel];
    [self addSubview:self.inputPassWordView];
    [self addSubview:self.inputNameView];
    [self.titleNavLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_offset(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleNavLabel.mas_bottom).with.offset(40);
        make.left.equalTo(self.mas_left).with.offset(40);
        make.right.equalTo(self.mas_right);
        make.height.mas_offset(40);
    }];
    [self.titleNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(14);
        make.right.equalTo(self.titleLabel.mas_right);
        make.left.equalTo(self.titleLabel.mas_left);
        make.height.mas_offset(15);
    }];
    
    [self.inputNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleNameLabel.mas_bottom);
        make.right.equalTo(self.mas_right);
        make.left.equalTo(self.mas_left);
        make.height.mas_offset(50);
    }];
    [self.titlePassWordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputNameView.mas_bottom).with.offset(14);
        make.right.equalTo(self.titleLabel.mas_right);
        make.left.equalTo(self.titleLabel.mas_left);
        make.height.mas_offset(15);
    }];
    
    [self.inputPassWordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlePassWordLabel.mas_bottom);
        make.right.equalTo(self.mas_right);
        make.left.equalTo(self.mas_left);
        make.height.mas_offset(50);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputPassWordView.mas_bottom).with.offset(50);
        make.right.equalTo(self.mas_right).with.offset(-40);
        make.left.equalTo(self.mas_left).with.offset(40);
        make.height.mas_offset(40);
    }];
    
    WEAKSELF
//    self.inputView.getTextFromeInputView = ^(NSString *str) {
//        STRONGSELF
//        [strongSelf inputHandling];
//    };
    self.inputNameView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        [strongSelf inputHandling];
    };
    self.inputPassWordView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        [strongSelf inputHandling];
    };
}

- (void)inputHandling{
    if (self.inputNameView.inputText.length <= 0 || self.inputPassWordView.inputText.length <= 0 ) {
        self.sureButton.backgroundColor = GlobalGrayOneColor;
        self.sureButton.userInteractionEnabled = NO;
    }else{
        
        self.sureButton.backgroundColor = GlobalBuleColor;
        self.sureButton.userInteractionEnabled = YES;
    }
    
}

- (UILabel *)titleNavLabel{
    if (!_titleNavLabel) {
        _titleNavLabel = [[UILabel alloc]init];
        _titleNavLabel.text = @"";
        _titleNavLabel.textAlignment = NSTextAlignmentCenter;
        _titleNavLabel.font = [UIFont systemFontOfSize:16];
    }
    return _titleNavLabel;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"设置用户名和密码";
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _titleLabel;
}

//- (XFDLogInInputView *)inputView{
//    if (!_inputView) {
//        _inputView =  [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
//        _inputView.placeholder = @"6-8位字符，英文字母,数字,下划线";
//    }
//    return _inputView;
//}
//- (UILabel *)titleUserNameLabel{
//    if (!_titleUserNameLabel) {
//        _titleUserNameLabel = [[UILabel alloc]init];
//        _titleUserNameLabel.text = @"用户名";
//        _titleUserNameLabel.textAlignment = NSTextAlignmentLeft;
//        _titleUserNameLabel.font = [UIFont systemFontOfSize:14];
//    }
//    return _titleUserNameLabel;
//}

- (UILabel *)titleNameLabel{
    if (!_titleNameLabel) {
        _titleNameLabel = [[UILabel alloc]init];
        _titleNameLabel.text = @"姓名";
        _titleNameLabel.textAlignment = NSTextAlignmentLeft;
        _titleNameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _titleNameLabel;
}
- (UILabel *)titlePassWordLabel{
    if (!_titlePassWordLabel) {
        _titlePassWordLabel = [[UILabel alloc]init];
        _titlePassWordLabel.text = @"密码";
        _titlePassWordLabel.textAlignment = NSTextAlignmentLeft;
        _titlePassWordLabel.font = [UIFont systemFontOfSize:14];
    }
    return _titlePassWordLabel;
}
- (XFDLogInInputView *)inputNameView{
    if (!_inputNameView) {
        _inputNameView =  [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
        _inputNameView.placeholder = @"请输入您的真实姓名";
    }
    return _inputNameView;
}


- (XFDLogInInputView *)inputPassWordView{
    if (!_inputPassWordView) {
        _inputPassWordView =  [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
        _inputPassWordView.placeholder = @"6-20位字母或数字";
        _inputPassWordView.isCiphertext = YES;
        _inputPassWordView.isCiphertButton = YES;
    }
    return _inputPassWordView;
}

- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"提交" forState:UIControlStateNormal];
        _sureButton.backgroundColor = GlobalGrayOneColor;
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sureButton.layer.cornerRadius = 5;
        _sureButton.layer.masksToBounds = YES;
        WEAKSELF
        [_sureButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObjectSafely:strongSelf.inputNameView.inputText forKey:@"name"];
            [dict setObjectSafely:strongSelf.inputPassWordView.inputText forKey:@"password"];
            if (strongSelf.commitBlock) {
                strongSelf.commitBlock(dict);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}
@end
